package top.continew.wechat.common.core;

import lombok.Data;

import java.io.Serializable;

/**
 * 通用响应结果
 */
@Data
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 状态码
     */
    private int code;

    /**
     * 消息
     */
    private String message;

    /**
     * 数据
     */
    private T data;

    /**
     * 成功
     */
    public static <T> Result<T> success() {
        return success(null);
    }

    /**
     * 成功
     */
    public static <T> Result<T> success(T data) {
        return success(ResultCode.SUCCESS.getMessage(), data);
    }

    /**
     * 成功
     */
    public static <T> Result<T> success(String message, T data) {
        return new Result<T>()
                .setCode(ResultCode.SUCCESS.getCode())
                .setMessage(message)
                .setData(data);
    }

    /**
     * 失败
     */
    public static <T> Result<T> failure() {
        return failure(ResultCode.FAILURE);
    }

    /**
     * 失败
     */
    public static <T> Result<T> failure(ResultCode resultCode) {
        return failure(resultCode, null);
    }

    /**
     * 失败
     */
    public static <T> Result<T> failure(ResultCode resultCode, T data) {
        return new Result<T>()
                .setCode(resultCode.getCode())
                .setMessage(resultCode.getMessage())
                .setData(data);
    }

    /**
     * 失败
     */
    public static <T> Result<T> failure(int code, String message) {
        return new Result<T>()
                .setCode(code)
                .setMessage(message);
    }

    /**
     * 设置状态码
     */
    public Result<T> setCode(int code) {
        this.code = code;
        return this;
    }

    /**
     * 设置消息
     */
    public Result<T> setMessage(String message) {
        this.message = message;
        return this;
    }

    /**
     * 设置数据
     */
    public Result<T> setData(T data) {
        this.data = data;
        return this;
    }
}
